import json

from ttzScrapyProject.constants.UrlConstant import CURRENT_LOTTERY_INFO_URL, INDEX_LOTTERY_INFO_URL
from ttzScrapyProject.convert import parseLotteryInfoItem
from ttzScrapyProject.dao.BaseDao import MysqlDao
from ttzScrapyProject.utils import HttpUtils, FileUtils

"""
奖池信息获取
"""


class LotteryDao(MysqlDao):
    def __init__(self):
        super().__init__()
        self.cursor = self.getCursor()
        self.db = self.getDB()

    def getDoingLotteryInfo(self, loginUserVo):
        doingLotteryInfoUrl = f"{CURRENT_LOTTERY_INFO_URL}{loginUserVo.usertoken}"
        doingLotteryInfoData = HttpUtils.get("获取当前期信息", doingLotteryInfoUrl)
        data = doingLotteryInfoData["Data"]
        print(f"当前正在开奖中的一期为{data}")
        # 期号当前期号
        lotteryIssue = data['LotteryIssue']
        # 最近一期的开奖信息
        lastLotteryIssue = data['Last']
        lotteryNum = data['LotteryNum']
        loginUserVo.setLuckNum(lotteryNum)
        lotteryNum1 = data['LotteryNum1']
        lotteryNum2 = data['LotteryNum2']
        lotteryNum3 = data['LotteryNum3']
        countDownFuck = data['CountDownFuck']
        FileUtils.writeCountDownFuck("CountDownFuck.txt", countDownFuck)

        loginUserVo.setLotteryIssue(lotteryIssue)
        loginUserVo.setLastLotteryIssue(lastLotteryIssue)
        loginUserVo.setCountDownFuck(countDownFuck)
        return loginUserVo

    def getIndexLotteryInfo(self, loginUserVo):
        indexLotteryInfoUrl = f"{INDEX_LOTTERY_INFO_URL}{loginUserVo.usertoken}"
        indexData = HttpUtils.get("获取首页开奖数据", indexLotteryInfoUrl)
        # print(f"全部的首页数据为{indexData}")
        doingData = indexData["Data"]["DoingLuckList"]
        # 已经开奖的历史数据
        data = indexData["Data"]["DoneLuckList"]
        # 所有的数据
        lotteryInfoList = parseLotteryInfoItem.parseLotteryInfo(self, data)
        loginUserVo.setLotteryInfoList(lotteryInfoList)

        # self.updateLotteryInfoList(loginUserVo, lotteryInfoList)
        return doingData
        ######### 模式?

    def updateLotteryInfoList(self, loginUserVo, lotteryInfoList):
        for item in lotteryInfoList:
            lotteryId = item['lotteryId']
            sql = f"select * from lottery_info where lottery_id ={lotteryId}"
            self.cursor.execute(sql)
            result = self.cursor.fetchall()
            if result:
                print(f"期号{lotteryId}已入库,无需操作,跳过插入")
            else:
                sql = "INSERT INTO `lottery_info` (`lottery_id`, `lottery_luck_num1`, `lottery_luck_num2`, `lottery_luck_num3`, `lottery_luck_num`, `lottery_open_time`)" \
                      "VALUES (%s, %s, %s, %s, %s, %s)"
                values = (item['lotteryId'], item['lotteryLuckNum1'], item['lotteryLuckNum2'], item['lotteryLuckNum3'],
                          item['lotteryLuckNum'], item['lottery_open_time']),
                self.cursor.executemany(sql, values)
            self.db.commit()


def parseDoingLotteryInfo(self, response):
    successCode = self.settings.get("SUCCESS_CODE")

    data = json.loads((response.body))
    print(data["Code"])
    if data["Code"] == successCode:
        return data
    else:
        return ""


""" 首页开奖信息的获取
"""


def parseIndexDataInfo(self, response):
    successCode = self.settings.get("SUCCESS_CODE")

    data = json.loads((response.body))
    print(data["Code"])
    if data["Code"] == successCode:
        return data
    else:
        return ""
